package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.domain.UserAddr;
import com.powernode.mapper.UserAddrMapper;
import com.powernode.service.UserAddrService;
import com.powernode.utils.AuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Date;
import java.util.List;
@Service
public class UserAddrServiceImpl extends ServiceImpl<UserAddrMapper, UserAddr> implements UserAddrService{
    @Autowired
    private UserAddrMapper userAddrMapper;

    @Override
    public List<UserAddr> selectUserAddrList(String userId) {
        List<UserAddr> userAddrList = userAddrMapper.selectList(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, userId).eq(UserAddr::getStatus, 1)
                .orderByDesc(UserAddr::getCommonAddr, UserAddr::getUpdateTime));
        return userAddrList;
    }

    @Override
    public void setDefaultAddr(Long addrId) {
        UserAddr userAddr = userAddrMapper.selectOne(new LambdaQueryWrapper<UserAddr>().eq(UserAddr::getAddrId, addrId));
        Integer isDefault = userAddr.getCommonAddr();
        if (isDefault == 1) return;

        String userId = AuthUtil.getLoginUserId();
        UserAddr defaultAddr = userAddrMapper.selectOne(new LambdaQueryWrapper<UserAddr>()
                .eq(UserAddr::getUserId, userId)
                .eq(UserAddr::getCommonAddr, 1));
        defaultAddr.setCommonAddr(0);
        userAddr.setCommonAddr(1);

        userAddrMapper.updateById(defaultAddr);
        userAddrMapper.updateById(userAddr);
    }

    @Override
    public boolean save(UserAddr entity) {
        // 判断当前用户是否有收获地址，如果有则不会是默认的，如果没有则改为默认
        List<UserAddr> userAddrList = userAddrMapper.selectList(new LambdaQueryWrapper<UserAddr>().eq(UserAddr::getUserId, entity.getUserId())
                .eq(UserAddr::getStatus, 1));
        entity.setCommonAddr(0);
        if (CollectionUtil.isEmpty(userAddrList) || userAddrList.size() == 0) {
            entity.setCommonAddr(1);
        }

        entity.setCreateTime(new Date());
        entity.setVersion(0);
        entity.setUpdateTime(new Date());
        entity.setStatus(1);

        return userAddrMapper.insert(entity) > 0;
    }

    @Override
    public boolean removeById(Serializable addrId) {
        String userId = AuthUtil.getLoginUserId();
        UserAddr userAddr = userAddrMapper.selectOne(new LambdaQueryWrapper<UserAddr>().eq(UserAddr::getAddrId, addrId));
        Integer isDefault = userAddr.getCommonAddr();

        int count = userAddrMapper.delete(new LambdaQueryWrapper<UserAddr>().eq(UserAddr::getAddrId, addrId));
        if (isDefault == 1) {
            List<UserAddr> userAddrList = userAddrMapper.selectList(new LambdaQueryWrapper<UserAddr>()
                    .eq(UserAddr::getUserId, userId)
                    .eq(UserAddr::getStatus, 1)
                    .orderByDesc(UserAddr::getCreateTime));
            if (CollectionUtil.isNotEmpty(userAddrList) && userAddrList.size() != 0) {
                UserAddr defaultUserAddr = userAddrList.get(0);
                defaultUserAddr.setCommonAddr(1);
                userAddrMapper.updateById(defaultUserAddr);
            }

        }
        return count > 0;
    }
}
